#include<iostream>
#include"Common.hpp"
#include<cstdio>
void Usage(std::string argv){
    std::cout<<"Usage: "<<argv<<" <server ip> <server port>"<<std::endl;
}
int main(int argc,char*argv[]){
    if(argc<3){
        Usage(argv[0]);
        return 1;
    }
    if(argc>3){
        LOG(LogLevel::WARNING)<<"参数过多";
    }
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0){
        LOG(LogLevel::ERROR)<<"Socket creation failed";
        return 1;
    }
    //bind,不需要显示绑定也不需要监听
    std::string serverip=argv[1];
    uint16_t serverport=std::stoi(argv[2]);
    //直接发起连接请求,请求成功后会自动发起绑定
    NetAddr peer(serverip,serverport);
    if(connect(sockfd,(struct sockaddr*)&peer.getAddr(),sizeof(peer.getAddr()))<0){
        LOG(LogLevel::ERROR)<<"Connect failed";
        return 1;

    }
    while(true){
        std::string line;
        std::cout<<"#please Enter message:"<<std::endl;
        std::getline(std::cin,line);
        if(line=="quit"){
            break;
        }
        write(sockfd,line.c_str(),line.size());
        char recvbuf[1024];
        int recvlen=read(sockfd,recvbuf,1024);
        if(recvlen<0){
            LOG(LogLevel::ERROR)<<"Read failed";
            return 1;
        }
        std::cout<<"Recv:"<<recvbuf<<std::endl;
    }
    close(sockfd);
    return 0;
}
   